# -*- coding:utf-8 -*-

from jsonschema import validate
import json

class JsonVerifies():
    def json_validate(self,jsonstr,schema):
        if isinstance(jsonstr,dict) or isinstance(jsonstr,list) or isinstance(jsonstr,tuple):
            jsonstr=json.dumps(jsonstr)
        if is_json(jsonstr):
            decodejson=json.loads(jsonstr)
            validate(decodejson, schema)
        else:
            print "json格式错误!"

    def create_schema_simple(self,**desc):
        '''
        properties desc

        exp.{
        "name": {
                "type": "string",
                "minimum": 16,
                "maximum": 512,
                "oneOf": [
                    {"format": "host-name"},
                    {"format": "ipv4"},
                    {"format": "ipv6"}
                ]
            }
        }
        '''
        schema={
            "type":"object",
        }
        schema["properties"]=desc
        return schema





def is_json(myjson):
    try:
        json.loads(myjson)
    except ValueError:
        return False
    return True




if __name__ == '__main__':
    schema={
        "title": "Product",
        "description": "A product from Acme's catalog",
        "type":"object",
        "properties":{
            "price":{"description": "大神","type":"number"},
            "name":{"type":"string"}
        },
    }
    price={
        "data":{"name" : "Eggs", "price": "" },
        "result":"success"
        }
    desc={"name":{"type":"string"}}
    schema1=JsonVerifies().create_schema_simple()
    result=JsonVerifies().json_validate(price, schema)





